package com.springboot.dense.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.dense.dto.FogLevelDTO;
import com.springboot.dense.entity.FogLevel;
import com.springboot.dense.entity.MonitoringPoint;
import com.springboot.dense.service.FogLevelService;
import com.springboot.dense.service.MonitoringPointService;
import com.springboot.dense.util.PythonUtil;
import com.springboot.dense.vo.*;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-04-14
 */
@RestController
public class FogLevelCtrl {

    @Autowired
    private FogLevelService FogLevelService;

    @Autowired
    private MonitoringPointService monitoringPointService;

    @Autowired
    private PythonUtil pythonUtil;

    @GetMapping("/admin/FogLevelAll")
    public JSONObject FogLevelAll() {
        List<FogLevelDTO> FogLevelList=FogLevelService.findByAll();
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "获取成功！");
        jsonObject.put("FogLevel", FogLevelList);
        jsonObject.put("top", "top_fog");
        jsonObject.put("show_left", "left_fog");
        jsonObject.put("left", "left_y_fog");
        return jsonObject;
    }

    @PostMapping("/admin/FogLevelSearch")
    public JSONObject FogLevelSearch(String city, String type) {
        List<FogLevelDTO> FogLevelList=FogLevelService.findsearchAll(city,type);
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "获取成功！");
        jsonObject.put("FogLevel", FogLevelList);
        return jsonObject;
    }

    @PostMapping("/admin/FogLevelDelete")
    public JSONObject FogLevelDelete(Integer id) {
        FogLevelService.removeById(id);
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "删除成功！");
        return jsonObject;
    }

    @GetMapping("/admin/FogLevelToAdd")
    public JSONObject FogLevelToAdd() {
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "初始化");
        jsonObject.put("top", "top_fog");
        jsonObject.put("show_left", "left_addfog");
        jsonObject.put("left", "left_y_fog");
        return jsonObject;
    }

    @PostMapping("/admin/FogLevelAdd")
    public JSONObject FogLevelAdd(FogLevel FogLevel) {
        ArrayList result = pythonUtil.httpGet(FogLevel.getUrl());
        System.out.println(result);
        FogLevel.setValue(Float.parseFloat((String) result.get(0)));
        FogLevel.setLevel(Integer.parseInt((String) result.get(1)));
        FogLevelService.save(FogLevel);
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "添加成功！");
        return jsonObject;
    }

    @PostMapping("/admin/FogLevelOne")
    public JSONObject FogLevelOne(Integer id) {
        FogLevel FogLevelList= FogLevelService.getById(id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "查询成功！");
        jsonObject.put("FogLevel", FogLevelList);
        jsonObject.put("FogData", simpleDateFormat.format(FogLevelList.getDate()));
        jsonObject.put("top", "top_fog");
        jsonObject.put("show_left", "left_updatefog");
        jsonObject.put("left", "left_y_fog");
        return jsonObject;
    }

    @PostMapping("/admin/FogLevelUpdate")
    public JSONObject FogLevelUpdate(FogLevel FogLevel, Integer id) {
        FogLevel.setId(id);
        FogLevelService.updateById(FogLevel);
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "修改成功！");
        return jsonObject;
    }

/******************************************* 前（下） 后（上）台 分割线 ***************************************/

    @GetMapping("/font/fontFogLevelForMap")
    public List fontFogLevelAll(String date){
        List<CityAlertVO> FogLevelList;
        List resultList = new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (ObjectUtils.isEmpty(date)){
            FogLevelList = FogLevelService.findCityAlertByDate(dateFormat.format(new Date()));
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
            try {
                Date formatDate = sdf.parse(date);
                FogLevelList = FogLevelService.findCityAlertByDate(dateFormat.format(formatDate));
                FogLevelList.forEach(fogLevel -> {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("name",fogLevel.getCity());
                    jsonObject.put("value",fogLevel.getValue());
                    resultList.add(jsonObject);
                });
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return resultList;
    }

    /**
     * 根据日期查询
     * @return
     */
    @GetMapping("/font/cityAlert")
    public List<CityAlertVO> cityAlert(String date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<CityAlertVO> resultList = new ArrayList<>();
        if (ObjectUtils.isEmpty(date)){ //如果date为空的话，则返回当日的结果
            resultList = FogLevelService.findCityAlertByDate(dateFormat.format(new Date()));
        }else {//否则返回相应天的数据
            SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
            try{
                Date formatDate = sdf.parse(date);
                resultList = FogLevelService.findCityAlertByDate(dateFormat.format(formatDate));
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return resultList;
    }


    @GetMapping("/font/cityHistoryLevel")
    public List<CityHistoryLevelVO> cityHistoryLevel(){
        return FogLevelService.findCityHistoryLevel();
    }

    @GetMapping("/font/cityLastWeekLevel")
    public JSONObject cityLastWeekLevel(String city){
        System.out.println(city);
        List<CityLastWeekLevelVO> cityLastWeekLevelList = FogLevelService.findCityLastWeekLevel(city);
        List timeList = new ArrayList();
        List valueList = new ArrayList();
        cityLastWeekLevelList.forEach(cityInfo -> {
            timeList.add(cityInfo.getDate());
            valueList.add(cityInfo.getValue());
        });
        JSONObject jsonObject = new JSONObject();
        Collections.reverse(timeList);
        Collections.reverse(valueList);
        jsonObject.put("timeList",timeList);
        jsonObject.put("valueList",valueList);
        return jsonObject;
    }

    @GetMapping("/font/getAllCityLastWeekLevel")
    public List getAllCityLastWeekLevel(){
        System.out.println("111");
        List dateList = new ArrayList();
        dateList.add("product");
        List resultList = new ArrayList();
        //查询所有城市
        List<MonitoringPoint> monitoringPointList = monitoringPointService.list(null);
        List<CityHistoryDetailVO> cityLastWeekLevelList = FogLevelService.findCityLastWeekLevelTwo("南昌");
        cityLastWeekLevelList.forEach(cityInfo -> {
            dateList.add(cityInfo.getDate());
        });
        resultList.add(dateList);
        //遍历所有城市，通过城市名查询该城市的历史七天的数据
        monitoringPointList.forEach(monitoringPoint -> {
            List cityValueList = new ArrayList();
            cityValueList.add(monitoringPoint.getCity());
            List<CityHistoryDetailVO> cityList = FogLevelService.findCityLastWeekLevelTwo(monitoringPoint.getCity());
            cityList.forEach(cityInfo -> {
                cityValueList.add(cityInfo.getValue());
            });
            resultList.add(cityValueList);
        });
        return resultList;
    }


    /**
     * 根据日期和城市查询某个城市的具体数据
     */
    @GetMapping("/font/findOneCityDetail")
    public CityDetailVO findOneCityDetail(String date, @RequestParam("city") String city){
        if (ObjectUtils.isEmpty(date)){ //如果没有传日期数据，则生成今天的日期
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String today = simpleDateFormat.format(new Date());
            return FogLevelService.findOneCityDetail(today,city);
        }else { //有日期则返回那天的日期
            return FogLevelService.findOneCityDetail(date, city);
        }
    }

}

